library DatabaseLegacy

    globals
        private hashtable gameDataTable = InitHashtable()
        private hashtable playerDataTable= InitHashtable()
    endglobals
    
    //===========================================================================
    // Hastable (replaces the outdated gamecache system)
    // The new hashtables use two keys, the first key is the "master" table while the 2nd is the
    // subtable.
    //===========================================================================

	function GetUnitUserData2 takes unit u returns integer 
		return LoadInteger(gameDataTable, -GetHandleId(u), -1)
	endfunction
	
	function SetUnitUserData2 takes unit u, integer d returns nothing
		call SaveInteger(gameDataTable, -GetHandleId(u), -1, d )
	endfunction
	
    // cleans table under master key "h"
    function FlushHandleLocals takes handle h returns nothing 
         call FlushChildHashtableBJ( GetHandleId(h), gameDataTable )
    endfunction

    function putBoolean takes handle key_handle, string key_string, boolean value returns nothing 
         call SaveBooleanBJ(value, GetHandleId(key_handle), StringHash(key_string), gameDataTable )
    endfunction

    function getBoolean takes handle key_handle, string key_string returns boolean
        return LoadBooleanBJ(GetHandleId(key_handle), StringHash(key_string), gameDataTable )
    endfunction

    function putReal takes handle key_handle, string key_string, real value returns nothing
        call SaveRealBJ(value, GetHandleId(key_handle), StringHash(key_string), gameDataTable )
    endfunction

    function getReal takes handle key_handle, string key_string returns real
        return LoadRealBJ( GetHandleId(key_handle), StringHash(key_string), gameDataTable )
    endfunction

    function putInteger takes handle key_handle, string key_string, integer value returns nothing
        call SaveIntegerBJ(value, GetHandleId(key_handle), StringHash(key_string), gameDataTable )
    endfunction

    function getInteger takes handle key_handle, string key_string returns integer
        return LoadIntegerBJ( GetHandleId(key_handle), StringHash(key_string), gameDataTable )
    endfunction

    function putLocation takes handle key_handle, string key_string, location value  returns nothing 
        call SaveLocationHandleBJ( value, GetHandleId(key_handle), StringHash(key_string), gameDataTable )
    endfunction

    function getLocation takes handle key_handle, string key_string returns location
        return LoadLocationHandleBJ (GetHandleId(key_handle), StringHash(key_string), gameDataTable)
    endfunction

    function putUnit takes  handle key_handle, string key_string, unit value  returns nothing 
        call SaveUnitHandleBJ( value, GetHandleId(key_handle), StringHash(key_string), gameDataTable )
    endfunction

    function getUnit takes handle key_handle, string key_string returns unit
        return LoadUnitHandleBJ(GetHandleId(key_handle), StringHash(key_string), gameDataTable)
    endfunction
        
    function putPlayer takes handle key_handle, string key_string, player value returns nothing 
        call SavePlayerHandleBJ( value, GetHandleId(key_handle), StringHash(key_string), gameDataTable )
    endfunction

    function getPlayer takes handle key_handle, string key_string returns player
        return LoadPlayerHandleBJ(GetHandleId(key_handle), StringHash(key_string), gameDataTable)
    endfunction

    function putItem takes handle key_handle, string key_string, item value returns nothing 
          call SaveItemHandleBJ(value, GetHandleId(key_handle), StringHash(key_string), gameDataTable)
    endfunction

    function getItem takes handle key_handle, string key_string returns item
         return LoadItemHandleBJ(GetHandleId(key_handle), StringHash(key_string), gameDataTable)
    endfunction

    function putTimer takes handle key_handle, string key_string, timer value returns nothing 
         call SaveTimerHandleBJ(value, GetHandleId(key_handle), StringHash(key_string), gameDataTable)
    endfunction

    function getTimer takes handle key_handle, string key_string returns timer
          return LoadTimerHandleBJ(GetHandleId(key_handle), StringHash(key_string), gameDataTable)
    endfunction

    //===========================================================================
    // Some methods for easier interfacing 
    // (idk why azazel needed those for but they are used alot)
    //===========================================================================

	function HasI takes string key_string, integer key_integer returns boolean
         return HaveSavedInteger(gameDataTable, key_integer, StringHash(key_string))
    endfunction
	
    function GetI takes string key_string, integer key_integer returns integer
         return LoadInteger(gameDataTable, key_integer, StringHash(key_string))
    endfunction

    function SetI takes string key_string, integer key_integer, integer value returns nothing
         call SaveInteger(gameDataTable , key_integer, StringHash(key_string), value)
    endfunction

    function GetS takes string key_string, integer key_integer returns string
        return LoadStringBJ(StringHash(key_string), key_integer, gameDataTable )
    endfunction

    function SetS takes string key_string, integer key_integer, string value returns nothing
         call SaveStringBJ(value, StringHash(key_string), key_integer, gameDataTable )
    endfunction

    function GetB takes string key_string, integer key_integer returns boolean
        return LoadBooleanBJ(StringHash(key_string), key_integer, gameDataTable )
    endfunction

    function SetB takes string key_string, integer key_integer, boolean value returns nothing
         call SaveBooleanBJ(value, StringHash(key_string), key_integer, gameDataTable )
    endfunction



    //===========================================================================
    // Dynamic player data (exp, ammo, etc) stores integers/booleans only
    //===========================================================================

    function setPlayerDataInteger takes player p, string key, integer value returns nothing
           call SaveIntegerBJ(value, GetHandleId(p), StringHash(key), playerDataTable )
    endfunction

    function getPlayerDataInteger takes player p, string key returns integer
           return LoadIntegerBJ(GetHandleId(p), StringHash(key), playerDataTable )
    endfunction

    function setPlayerDataBoolean takes player p, string key, boolean value returns nothing
           call SaveBooleanBJ(value, GetHandleId(p), StringHash(key), playerDataTable )
    endfunction

    function getPlayerDataBoolean takes player p, string key returns boolean
           return LoadBooleanBJ(GetHandleId(p), StringHash(key), playerDataTable )
    endfunction

    //===========================================================================

    function GetLoadState takes string key_handle returns integer
        return LoadIntegerBJ(StringHash(key_handle), StringHash(key_handle), gameDataTable )
    endfunction

    function SetLoadState takes string key_handle, integer value returns nothing
        call SaveIntegerBJ(value, StringHash(key_handle), StringHash(key_handle), gameDataTable )
    endfunction

endlibrary 